package com.hotel.controller;

import com.hotel.common.Result;
import com.hotel.entity.SysUser;
import com.hotel.service.SysUserService;
import com.hotel.dto.LoginRequest;
import com.hotel.dto.LoginResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户控制器
 * 
 * @author Hotel Management Team
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        LoginResponse response = userService.login(request);
        return Result.success("登录成功", response);
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public Result<Void> logout(@RequestHeader("Authorization") String token) {
        userService.logout(token);
        return Result.success("登出成功");
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public Result<SysUser> getCurrentUser(@RequestHeader("Authorization") String token) {
        SysUser user = userService.getCurrentUser(token);
        return Result.success(user);
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public Result<List<SysUser>> getUserList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        List<SysUser> users = userService.getUserList(page, size, keyword);
        return Result.success(users);
    }

    /**
     * 创建用户
     */
    @PostMapping
    public Result<Void> createUser(@Valid @RequestBody SysUser user) {
        userService.createUser(user);
        return Result.success("用户创建成功");
    }

    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    public Result<Void> updateUser(@PathVariable Long id, @Valid @RequestBody SysUser user) {
        user.setId(id);
        userService.updateUser(user);
        return Result.success("用户更新成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success("用户删除成功");
    }

    /**
     * 重置密码
     */
    @PostMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable Long id) {
        userService.resetPassword(id);
        return Result.success("密码重置成功");
    }

    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    public Result<Void> changePassword(
            @RequestHeader("Authorization") String token,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        userService.changePassword(token, oldPassword, newPassword);
        return Result.success("密码修改成功");
    }

    /**
     * 启用/禁用用户
     */
    @PostMapping("/{id}/toggle-status")
    public Result<Void> toggleUserStatus(@PathVariable Long id) {

        userService.disableUser(id);
        return Result.success("用户状态更新成功");
    }

}